package com.mk.controller.admin;

import cn.hutool.core.bean.BeanUtil;
import com.mk.constant.RedisCacheConstant;
import com.mk.dto.InfoDTO;
import com.mk.entity.Article;
import com.mk.entity.Info;
import com.mk.result.Result;
import com.mk.service.IInformationService;
import com.mk.utils.RedisUtil;
import com.mk.vo.InfoVO;
import lombok.RequiredArgsConstructor;
import org.apache.ibatis.annotations.Options;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;

/**
 * 资讯中心相关业务
 */
@RestController("adminInformationController")
@RequestMapping("/admin/info")
@RequiredArgsConstructor
public class InformationController {
    
    private final IInformationService informationService;
    private final RedisUtil redisUtil;
    private final HashMap<String,String> hm = new HashMap<>();

    /**
     * 新发布一条资讯
     * @param infoDTO
     * @return
     */
    @CrossOrigin
    @PostMapping("/add")
    @Options(keyProperty = "id", useGeneratedKeys = true)
    public Result addInfo(@RequestBody InfoDTO infoDTO) {
        Info info = new Info();
        BeanUtil.copyProperties(infoDTO, info);
        info.setCreateTime(LocalDateTime.now());
        info.setUpdateTime(LocalDateTime.now());
        informationService.save(info);

        if (!hm.isEmpty()) hm.clear();
        putInfo(Info.TITLE, info.getTitle());
        putInfo(Info.TEXT, info.getText());
        putInfo(Info.IMAGE, info.getImage());
        putInfo(Info.CREATE_TIME, info.getCreateTime().toString());
        putInfo(Info.UPDATE_TIME, info.getUpdateTime().toString());
        redisUtil.add(RedisCacheConstant.INFORMATION_KEY + info.getId(), hm);

        return Result.success();
    }

    /**
     * 删除一条资讯
     * @param infoId
     * @return
     */
    @CrossOrigin
    @DeleteMapping("/delete/{infoId}")
    public Result deleteInfo(@PathVariable Long infoId) {
        Map<Object, Object> map = redisUtil.getHashEntries(RedisCacheConstant.INFORMATION_KEY + infoId);
        if (!map.isEmpty()) redisUtil.delete(RedisCacheConstant.INFORMATION_KEY + infoId);
        informationService.removeById(infoId);
        return Result.success();
    }

    /**
     * 修改一条资讯
     * @param info
     * @return
     */
    @CrossOrigin
    @PutMapping("/update")
    public Result updateInfo(@RequestBody Info info) {
        info.setUpdateTime(LocalDateTime.now());
        Map<Object, Object> map = redisUtil.getHashEntries(RedisCacheConstant.INFORMATION_KEY + info.getId());
        if (!map.isEmpty()) {
            String title = map.get(Info.TITLE).toString();
            String text = map.get(Info.TEXT).toString();
            String image = map.get(Info.IMAGE).toString();
            LocalDateTime createTime = LocalDateTime.parse(map.get(Info.CREATE_TIME).toString());

            if (info.getTitle().isEmpty()) info.setTitle(title);
            if (info.getText().isEmpty()) info.setText(text);
            if (info.getImage().isEmpty()) info.setImage(image);
            info.setCreateTime(createTime);

            redisUtil.delete(RedisCacheConstant.INFORMATION_KEY + info.getId());
            if (!hm.isEmpty()) hm.clear();
            putInfo(Info.TITLE, info.getTitle());
            putInfo(Info.TEXT, info.getText());
            putInfo(Info.IMAGE, info.getImage());
            putInfo(Info.CREATE_TIME, info.getCreateTime().toString());
            putInfo(Info.UPDATE_TIME, info.getUpdateTime().toString());
            redisUtil.add(RedisCacheConstant.INFORMATION_KEY + info.getId(), hm);
        }

        informationService.updateById(info);
        return Result.success();
    }

    /**
     * 查询一条资讯
     * @param infoId
     * @return
     */
    @CrossOrigin
    @GetMapping("/get/{infoId}")
    public Result<InfoVO> getById(@PathVariable Long infoId) {
        Map<Object, Object> map = redisUtil.getHashEntries(RedisCacheConstant.INFORMATION_KEY + infoId);
        if (!map.isEmpty()) {
            String title = map.get(Info.TITLE).toString();
            String text = map.get(Info.TEXT).toString();
            String image = map.get(Info.IMAGE).toString();
            LocalDateTime createTime = LocalDateTime.parse(map.get(Info.CREATE_TIME).toString());
            LocalDateTime updateTime = LocalDateTime.parse(map.get(Info.UPDATE_TIME).toString());

            return Result.success(BeanUtil.copyProperties(Info.builder()
                    .title(title)
                    .text(text)
                    .image(image)
                    .createTime(createTime)
                    .updateTime(updateTime)
                    .build(), InfoVO.class));
        } else {
            return Result.success(BeanUtil.copyProperties(informationService.getById(infoId), InfoVO.class));
        }
    }

    //TODO 网站资讯的分页查询

    //TODO 用户还未发布但是要退出发布页面，保存或者不保存内容

    private void putInfo(String key, String value) {
        hm.put(key, value);
    }
}
